<html>
<head><meta charset="utf-8"><title>How do I get the module that an Item belongs to? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20that.20an.20Item.20belongs.20to.3F.html">How do I get the module that an Item belongs to?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199982229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20that%20an%20Item%20belongs%20to%3F/near/199982229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20that.20an.20Item.20belongs.20to.3F.html#199982229">(Jun 06 2020 at 16:15)</a>:</h4>
<p>I managed to get a DefId for it but I'm not sure where to go from there.</p>



<a name="199982882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20that%20an%20Item%20belongs%20to%3F/near/199982882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20that.20an.20Item.20belongs.20to.3F.html#199982882">(Jun 06 2020 at 16:30)</a>:</h4>
<p><code>tcx.parent(def_id)</code> using the <code>DefIdTree</code> trait will get the parent item of a def_id. For items this will be the parent module.<br>
If you may have an associated item or something that's not an item, you'll have to call <code>parent</code> in a loop until <code>tcx.def_kind(def_id)</code> is <code>DefKind::Mod</code>.<br>
If the item is guaranteed to be in the current crate, then <code>parent_module</code> on the <code>HirId</code> would be easiest.</p>



<a name="199983259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20that%20an%20Item%20belongs%20to%3F/near/199983259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20that.20an.20Item.20belongs.20to.3F.html#199983259">(Jun 06 2020 at 16:41)</a>:</h4>
<blockquote>
<p>If the item is guaranteed to be in the current crate, then parent_module on the HirId would be easiest.</p>
</blockquote>
<p>This is actually <em>only</em> for cross-crate re-exports. <code>tcx.parent()</code> looks like what I need, thank you!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>